package com.itcast.database.smooth.config;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;

/**
 * 分库配置规则
 */
public class ShardingDataSourceRule implements PreciseShardingAlgorithm<Long> {

    /**
     * 分片规则， 取模运算
     */
    public static int MOD = 1;

    /**
     * 根据订单ID做分库处理
     * @param names
     * @param value
     * @return
     */
    @Override
    public String doSharding(Collection<String> names, PreciseShardingValue<Long> preciseShardingValue) {
        Long accountNo = preciseShardingValue.getValue();
        String dataSource = DatasourceEnum.DATASOURCE_PREFIX.getValue() + accountNo % MOD;
        return dataSource;
    }
}
